package com.wgod.demo.business.service;

import com.wgod.security.common.biz.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wgod.demo.business.entity.OptionsEntity;
import com.wgod.demo.business.mapper.OptionMapper;
import com.wgod.demo.common.constant.ErrorConstant;
import com.wgod.demo.common.exception.BusinessException;

import java.util.List;
import java.util.Map;

/**
 * 网站配置服务层
 * Created by WGod on 2018/4/28.
 */
@Service
public class OptionService extends BaseService<OptionMapper,OptionsEntity> {

    public void deleteOptionByName(String name) {
        if(StringUtils.isBlank(name))
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        mapper.deleteOptionByName(name);

    }

    @Transactional
    public void updateOptionByName(String name, String value) {
        if(StringUtils.isBlank(name))
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        OptionsEntity option = new OptionsEntity();
        option.setName(name);
        option.setValue(value);
        mapper.updateOptionByName(option);

    }

    @Transactional
    public void saveOptions(Map<String, String> options) {
        if (null != options && !options.isEmpty()) {
            options.forEach(this::updateOptionByName);
        }
    }

    public OptionsEntity getOptionByName(String name) {
        if(StringUtils.isBlank(name))
            throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
        return mapper.getOptionByName(name);
    }

    public List<OptionsEntity> getOptions() {
        return mapper.getOptions();
    }
}
